package com.sample.dubbo.client.config;

import com.alibaba.dubbo.config.*;
import com.alibaba.dubbo.config.spring.AnnotationBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zwg
 * @date 2018-11-12 11:37
 **/
@Configuration
public class DubboConfig {

    // 应用名称
    public static final String APPLICATION_NAME = "dubbo-client";
    // 扫描service包路径 多个路径需要逗号分隔
    public static final String ANNOTATION_PACKAGE = "com.sample.dubbo.client";

    /**
     * 以下信息将从配置文件application.properties中读取
      */

    @Value("${dubbo.registry.address}")
    private String registryAddress="localhost:2181";
    @Value("${dubbo.port:30001}")
    private int dubboPort;
    @Value("${dubbo.register:true}")
    private boolean dubboRegister;

    /**
     * 提供方应用信息，用于计算依赖关系
     *
     * @return
     */
    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(APPLICATION_NAME);
        return applicationConfig;
    }

    /**
     * 与<dubbo:annotation/>相当.
     * 提供方扫描带有@com.alibaba.dubbo.config.annotation.
     * Service的注解类
     *
     * @return
     */
    @Bean
    public static AnnotationBean annotationBean() {
        AnnotationBean annotationBean = new AnnotationBean();
        annotationBean.setPackage(ANNOTATION_PACKAGE);
        return annotationBean;
    }

    /**
     * 使用zookeeper注册中心暴露服务地址
     *
     * @return
     */
    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setProtocol("zookeeper");
        registryConfig.setAddress("127.0.0.1");
        registryConfig.setPort(2181);
        registryConfig.setCheck(false);
        registryConfig.setRegister(dubboRegister);
        return registryConfig;
    }

    /**
     * 用dubbo协议在指定端口暴露服务
     *
     * @return
     */
    @Bean
    public ProtocolConfig protocolConfig() {
        ProtocolConfig protocolConfig = new ProtocolConfig("dubbo", dubboPort);
        // 默认为hessian2,但不支持无参构造函数类,而这种方式的效率很低
        protocolConfig.setSerialization("java");
        return protocolConfig;
    }

    /**
     * 监控中心配置，protocol="registry"，表示从注册中心发现监控中心地址
     *
     * @return
     */
    @Bean
    public MonitorConfig monitorConfig() {
        MonitorConfig monitorConfig = new MonitorConfig();
        monitorConfig.setProtocol("registry");
        return monitorConfig;
    }

    /**
     * 当ProtocolConfig和ServiceConfig某属性没有配置时,采用此缺省值
     *
     * @return
     */
    @Bean
    public ProviderConfig providerConfig() {
        ProviderConfig providerConfig = new ProviderConfig();
        providerConfig.setTimeout(10000);
        providerConfig.setThreadpool("fixed");
        providerConfig.setThreads(100);
        providerConfig.setAccepts(1000);
        return providerConfig;
    }
}
